﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Ajeelee.Core.Utils;
using BoCaiWeb.Business;
using BoCaiWeb.Model;
using BoCaiWeb.WebSite.Code;

namespace BoCaiWeb.WebSite.WebAdmin
{
    public partial class ProductsEdit : AuthPageBase
    {
        protected int id;

        protected Products productsInfo = new Products();
        IProductsBusiness productsBll = new ProductsBusinessFactory().CreateProductsBusiness();

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            id = QueryParamInt32("id");
            try
            {
                if (id > 0)
                {
                    productsInfo = productsBll.GetById(id);
                    if (productsInfo == null)
                    {
                        productsInfo = new Products();
                        MessageBoxGo("不存在该信息", FormParam("sourceurl", "ProductsList.aspx"));
                    }
                }
                if (IsPost)
                {
                    if (id > 0)   // 更新
                    {
                        initModel();
                        productsBll.Update(productsInfo);
                        System.Threading.Thread.Sleep(500);
                        Page_GoTo(FormParam("sourceurl", "ProductsList.aspx"), true);

                    }
                    else          // 新建
                    {
                        initModel();
                        productsBll.Insert(productsInfo);
                        System.Threading.Thread.Sleep(500);
                        Page_GoTo(FormParam("sourceurl", "ProductsList.aspx"), true);
                    }
                }
            }
            catch (Exception ex)
            {
                Function.WriteErrorLog(ex);
            }
        }

        public void initModel()
        {
            if (id <= 0)
            {
                productsInfo = new Products();
                productsInfo.Author = User.Identity.Name;
                productsInfo.InTime = DateTime.Now;
                productsInfo.InIP = IPAddress;
                productsInfo.State = 2;
                productsInfo.Count = 0;
                productsInfo.CId = 0;
            }
            productsInfo.Title = FormParam("title");
            productsInfo.Pic = FormParam("faceImg");
            productsInfo.Syno = FormParam("summary");
            productsInfo.Content = FormParam("content");
            productsInfo.Top = FormParam("top");
            productsInfo.Url = FormParam("url");

            productsInfo.UpTime = DateTime.Now;
            productsInfo.UpIP = IPAddress;
        }
    }
}